package com.hiwan.system.security.filter;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.hiwan.platform.utils.DateUtils;
import com.hiwan.platform.utils.StringUtils;
import com.hiwan.system.pojo.LoginInfo;
import com.hiwan.system.service.login.LoginService;
import com.hiwan.system.util.UserCacheUtils;

@Component
public class LogoutFilter extends org.apache.shiro.web.filter.authc.LogoutFilter{
	
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private LoginService loginService;
	
	@Override
	protected boolean preHandle(ServletRequest request, ServletResponse response) throws Exception {
		LoginInfo loginInfo = new LoginInfo();
		String loginId = (String)UserCacheUtils.getSession().getAttribute("loginId");
		if(StringUtils.isNotBlank(loginId)){
			loginInfo.setLoginId(loginId);
			loginInfo.setLogoutTime(DateUtils.getDateTime());
			try {
				loginService.modify(loginInfo);
			} catch (Exception e) {
				logger.warn("登出日志记录错误{}，错误信息{}", loginInfo.getLoginId(),e);
			}
		}
		return super.preHandle(request, response);
	}
	
}
